package org.jeecg.modules.job.wms.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.wms.entity.WmsProExp;
import org.jeecg.modules.job.wms.entity.WmsWorkExp;
import org.jeecg.modules.job.wms.entity.WmsEduExp;
import org.jeecg.modules.job.wms.entity.WmsJobExpect;
import org.jeecg.modules.job.wms.entity.WmsResumeFile;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.jeecg.modules.job.wms.vo.WmsResumePage;
import org.jeecg.modules.job.wms.service.IWmsResumeService;
import org.jeecg.modules.job.wms.service.IWmsProExpService;
import org.jeecg.modules.job.wms.service.IWmsWorkExpService;
import org.jeecg.modules.job.wms.service.IWmsEduExpService;
import org.jeecg.modules.job.wms.service.IWmsJobExpectService;
import org.jeecg.modules.job.wms.service.IWmsResumeFileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 个人简历
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Api(tags="个人简历")
@RestController
@RequestMapping("/wms/wmsResume")
@Slf4j
public class WmsResumeController {
	@Autowired
	private IWmsResumeService wmsResumeService;
	@Autowired
	private IWmsProExpService wmsProExpService;
	@Autowired
	private IWmsWorkExpService wmsWorkExpService;
	@Autowired
	private IWmsEduExpService wmsEduExpService;
	@Autowired
	private IWmsJobExpectService wmsJobExpectService;
	@Autowired
	private IWmsResumeFileService wmsResumeFileService;
	
	/**
	 * 分页列表查询
	 *
	 * @param wmsResume
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "个人简历-分页列表查询")
	@ApiOperation(value="个人简历-分页列表查询", notes="个人简历-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<WmsResume>> queryPageList(WmsResume wmsResume,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WmsResume> queryWrapper = QueryGenerator.initQueryWrapper(wmsResume, req.getParameterMap());
		Page<WmsResume> page = new Page<WmsResume>(pageNo, pageSize);
		IPage<WmsResume> pageList = wmsResumeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param wmsResumePage
	 * @return
	 */
	@AutoLog(value = "个人简历-添加")
	@ApiOperation(value="个人简历-添加", notes="个人简历-添加")
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WmsResumePage wmsResumePage) {
		WmsResume wmsResume = new WmsResume();
		BeanUtils.copyProperties(wmsResumePage, wmsResume);
		wmsResumeService.saveMain(wmsResume, wmsResumePage.getWmsProExpList(),wmsResumePage.getWmsWorkExpList(),wmsResumePage.getWmsEduExpList(),wmsResumePage.getWmsJobExpectList(),wmsResumePage.getWmsResumeFileList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param wmsResumePage
	 * @return
	 */
	@AutoLog(value = "个人简历-编辑")
	@ApiOperation(value="个人简历-编辑", notes="个人简历-编辑")
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WmsResumePage wmsResumePage) {
		WmsResume wmsResume = new WmsResume();
		BeanUtils.copyProperties(wmsResumePage, wmsResume);
		WmsResume wmsResumeEntity = wmsResumeService.getById(wmsResume.getId());
		if(wmsResumeEntity==null) {
			return Result.error("未找到对应数据");
		}
		wmsResumeService.updateMain(wmsResume, wmsResumePage.getWmsProExpList(),wmsResumePage.getWmsWorkExpList(),wmsResumePage.getWmsEduExpList(),wmsResumePage.getWmsJobExpectList(),wmsResumePage.getWmsResumeFileList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "个人简历-通过id删除")
	@ApiOperation(value="个人简历-通过id删除", notes="个人简历-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		wmsResumeService.delMain(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "个人简历-批量删除")
	@ApiOperation(value="个人简历-批量删除", notes="个人简历-批量删除")
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wmsResumeService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "个人简历-通过id查询")
	@ApiOperation(value="个人简历-通过id查询", notes="个人简历-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<WmsResume> queryById(@RequestParam(name="id",required=true) String id) {
		WmsResume wmsResume = wmsResumeService.getById(id);
		if(wmsResume==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(wmsResume);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "项目经历通过主表ID查询")
	@ApiOperation(value="项目经历主表ID查询", notes="项目经历-通主表ID查询")
	@GetMapping(value = "/queryWmsProExpByMainId")
	public Result<List<WmsProExp>> queryWmsProExpListByMainId(@RequestParam(name="id",required=true) String id) {
		List<WmsProExp> wmsProExpList = wmsProExpService.selectByMainId(id);
		return Result.OK(wmsProExpList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "工作经历通过主表ID查询")
	@ApiOperation(value="工作经历主表ID查询", notes="工作经历-通主表ID查询")
	@GetMapping(value = "/queryWmsWorkExpByMainId")
	public Result<List<WmsWorkExp>> queryWmsWorkExpListByMainId(@RequestParam(name="id",required=true) String id) {
		List<WmsWorkExp> wmsWorkExpList = wmsWorkExpService.selectByMainId(id);
		return Result.OK(wmsWorkExpList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "教育经历通过主表ID查询")
	@ApiOperation(value="教育经历主表ID查询", notes="教育经历-通主表ID查询")
	@GetMapping(value = "/queryWmsEduExpByMainId")
	public Result<List<WmsEduExp>> queryWmsEduExpListByMainId(@RequestParam(name="id",required=true) String id) {
		List<WmsEduExp> wmsEduExpList = wmsEduExpService.selectByMainId(id);
		return Result.OK(wmsEduExpList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "工作经历通过主表ID查询")
	@ApiOperation(value="工作经历主表ID查询", notes="工作经历-通主表ID查询")
	@GetMapping(value = "/queryWmsJobExpectByMainId")
	public Result<List<WmsJobExpect>> queryWmsJobExpectListByMainId(@RequestParam(name="id",required=true) String id) {
		List<WmsJobExpect> wmsJobExpectList = wmsJobExpectService.selectByMainId(id);
		return Result.OK(wmsJobExpectList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "简历附件通过主表ID查询")
	@ApiOperation(value="简历附件主表ID查询", notes="简历附件-通主表ID查询")
	@GetMapping(value = "/queryWmsResumeFileByMainId")
	public Result<List<WmsResumeFile>> queryWmsResumeFileListByMainId(@RequestParam(name="id",required=true) String id) {
		List<WmsResumeFile> wmsResumeFileList = wmsResumeFileService.selectByMainId(id);
		return Result.OK(wmsResumeFileList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param wmsResume
    */
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WmsResume wmsResume) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<WmsResume> queryWrapper = QueryGenerator.initQueryWrapper(wmsResume, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //配置选中数据查询条件
      String selections = request.getParameter("selections");
      if(oConvertUtils.isNotEmpty(selections)) {
         List<String> selectionList = Arrays.asList(selections.split(","));
         queryWrapper.in("id",selectionList);
      }
      //Step.2 获取导出数据
      List<WmsResume> wmsResumeList = wmsResumeService.list(queryWrapper);

      // Step.3 组装pageList
      List<WmsResumePage> pageList = new ArrayList<WmsResumePage>();
      for (WmsResume main : wmsResumeList) {
          WmsResumePage vo = new WmsResumePage();
          BeanUtils.copyProperties(main, vo);
          List<WmsProExp> wmsProExpList = wmsProExpService.selectByMainId(main.getId());
          vo.setWmsProExpList(wmsProExpList);
          List<WmsWorkExp> wmsWorkExpList = wmsWorkExpService.selectByMainId(main.getId());
          vo.setWmsWorkExpList(wmsWorkExpList);
          List<WmsEduExp> wmsEduExpList = wmsEduExpService.selectByMainId(main.getId());
          vo.setWmsEduExpList(wmsEduExpList);
          List<WmsJobExpect> wmsJobExpectList = wmsJobExpectService.selectByMainId(main.getId());
          vo.setWmsJobExpectList(wmsJobExpectList);
          List<WmsResumeFile> wmsResumeFileList = wmsResumeFileService.selectByMainId(main.getId());
          vo.setWmsResumeFileList(wmsResumeFileList);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "个人简历列表");
      mv.addObject(NormalExcelConstants.CLASS, WmsResumePage.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("个人简历数据", "导出人:"+sysUser.getRealname(), "个人简历"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("org.jeecg.modules.job:wms_resume:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          // 获取上传文件对象
          MultipartFile file = entity.getValue();
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<WmsResumePage> list = ExcelImportUtil.importExcel(file.getInputStream(), WmsResumePage.class, params);
              for (WmsResumePage page : list) {
                  WmsResume po = new WmsResume();
                  BeanUtils.copyProperties(page, po);
                  wmsResumeService.saveMain(po, page.getWmsProExpList(),page.getWmsWorkExpList(),page.getWmsEduExpList(),page.getWmsJobExpectList(),page.getWmsResumeFileList());
              }
              return Result.OK("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.OK("文件导入失败！");
    }

}
